package com.offline.mall.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import shuishijie.suishijie.R;
import shuishijie.suishijie.WebViewActivity;
import shuishijie.suishijie.model.GloblaData;
import shuishijie.suishijie.util.WechartUtil;
import shuishijie.suishijie.view.WebBrowseActivity;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        Log.e("TTTT","111111");
        api = WXAPIFactory.createWXAPI(this, WechartUtil.APPID);
      //  api.registerApp(WechartUtil.APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

//        Intent intent =new Intent(WXPayEntryActivity.this,WebViewActivity.class);
//        intent.putExtra("type","1");
//        intent.putExtra("url","https://offline.gvg666.com/dist/index.html#/load/allOrders");
//        startActivity(intent);
//        // finish();
//        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
      // Logger.i("onPayFinish, errCode = " + resp.errCode + " " + resp.errStr);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("提示");
//            builder.setMessage("微信支付结果：" + resp.errCode);
//            builder.setOnDismissListener(d -> finish());
//            builder.show();
            String result = "支付失败";
            if (resp.errCode == 0) {
                result = "支付成功";
               GloblaData.setWechartPay(2);
            }
            Log.e("TTTTTTT","1111");
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();
            Intent intent =new Intent(WXPayEntryActivity.this,WebBrowseActivity.class);
            intent.putExtra("type","1");
            intent.putExtra("url","https://offline.gvg666.com/dist/index.html#/load/allOrders");
            startActivity(intent);
           // finish();
            finish();
        }
    }
}